package com.xiaoma.common.utils

import android.content.Context
import android.provider.Settings
import android.util.Log
import android.view.Window

/**
 * author: liangjingbo
 * date: 2023/10/27
 * describe:
 */
object ScreenUtils {

    private val TAG = this.javaClass.simpleName + "_XMTag"

    /**
     * 改变当前 Window 亮度
     * @param window Window 当前 window
     * @param brightness Float 亮度值，小于0表示使用首选屏幕亮度。0到1将亮度从暗调整为全亮。
     */
    fun changeWindowBrightness(
        window: Window,
        brightness: Float
    ) {
        Log.i(TAG, "changeWindowBrightness() brightness: $brightness")
        val attributes = window.attributes
        attributes.screenBrightness = brightness
        window.attributes = attributes
    }

    /**
     * 获取屏幕当前亮度
     * @param window Window 当前 window
     * @return Float 亮度值
     */
    fun getWindowBrightness(window: Window): Float {
        val attributes = window.attributes
        return attributes.screenBrightness
    }

    /**
     * 获取系统屏幕的亮度
     * @param context Context 上下文
     * @return Float 系统屏幕的亮度
     */
    fun getSystemScreenBrightness(context: Context): Float {
        return Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 0) / 255.0f
    }


}